package com.mspbots.sync.utils;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * com.mspbots.sync.utils.QueryParamsUtils
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/7/5
 */
public class WiseParamsUtils {

    public final static int DEFAULT_PAGE_SIZE = 500;

    /**
     * connect wise http request params build
     *
     * @param pageId      page index
     * @param queryParams request params string
     * @return MultiValueMap
     */
    public static MultiValueMap<String, String> queryParamsBuild(String pageId, String queryParams) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.set("pageId", pageId);
        params.set("pageSize", DEFAULT_PAGE_SIZE + "");
        if (!StringUtils.isEmpty(queryParams)) {
            params.set("conditions", queryParams);
        }
        return params;
    }

    /**
     * connect wise http request params build
     *
     * @param page        Starting with page 1, this is the number of pages available based on the current pagesize.
     * @param queryParams request params string
     * @return MultiValueMap
     */
    public static MultiValueMap<String, String> paginationBuild(String page, String queryParams) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.set("page", page);
        params.set("pageSize", DEFAULT_PAGE_SIZE + "");
        if (!StringUtils.isEmpty(queryParams)) {
            params.set("conditions", queryParams);
        }
        return params;
    }

    /**
     * connect wise http request params build
     *
     * @param queryParams request params string
     * @return MultiValueMap
     */
    public static MultiValueMap<String, String> mapToMultiValueMapBuild(Map<String, String> queryParams) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.setAll(queryParams);
        params.set("pageSize", DEFAULT_PAGE_SIZE + "");
        return params;
    }
}
